package com.moji.controller.api;


import com.moji.common.base.ApiResult;
import com.moji.entity.MjHomeProduct;
import com.moji.service.MjHomeProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 首页产品详情(MjHomeProduct)表控制层
 *
 * @author makejava
 * @since 2024-07-21 14:36:07
 */
@RestController
@RequestMapping("api/mjHomeProduct")
@Api(tags = "首页产品详情相关api接口", value = "首页产品详情相关api接口")
public class MjHomeProductController {
    /**
     * 服务对象
     */
    @Resource
    private MjHomeProductService mjHomeProductService;

    /**
     * 分页查询
     *
     * @param mjHomeProduct 筛选条件
     * @return 查询结果
     */
    @PostMapping("/queryByProductId")
    @ApiOperation(value = "根据产品Id获取首页产品详情，根据需要传相应的参数")
    public ApiResult<List<MjHomeProduct>> queryByProductId(@RequestBody  MjHomeProduct mjHomeProduct) {
        return ApiResult.ok(this.mjHomeProductService.queryByPage(mjHomeProduct));

    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("/detail")
    @ApiOperation(value = "根据id查看首页产品详情",response = MjHomeProduct.class)
    public ApiResult<MjHomeProduct> queryById(@RequestParam("id") Long id) {
        return ApiResult.ok(this.mjHomeProductService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param mjHomeProduct 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    @ApiOperation(value = "增加首页产品详情",response = MjHomeProduct.class)
    public ApiResult<MjHomeProduct> add(@Validated @RequestBody MjHomeProduct mjHomeProduct) {
        return ApiResult.ok(this.mjHomeProductService.insert(mjHomeProduct));
    }

    /**
     * 编辑数据
     *
     * @param mjHomeProduct 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    @ApiOperation(value = "修改首页产品详情",response = MjHomeProduct.class)
    public ApiResult<MjHomeProduct> edit(@Validated @RequestBody MjHomeProduct mjHomeProduct) {
        return ApiResult.ok(this.mjHomeProductService.update(mjHomeProduct));
    }



    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除首页产品详情或批量删除",response = Boolean.class)
    public ApiResult<Boolean> deleteById(@RequestParam("id") List<Long> id) {
        for (int i = 0; i < id.size(); i++) {
            Long aLong = id.get(i);
            if(!this.mjHomeProductService.deleteById(aLong))
            {
                return ApiResult.ok(false);
            }

        }
        return ApiResult.ok(true);
    }

}

